﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class DanhMuc_TruyVanDoanVien : System.Web.UI.Page
{
    string donvi = "";
    string level = "";
    _dbDataContext _db = new _dbDataContext();
    public SecurityLib.Sec_AES _ps = new SecurityLib.Sec_AES();
    protected void Page_Load(object sender, EventArgs e)
    {
        donvi = MemberShip_ASP.GetMaDonViFromUserName(Page.User.Identity.Name);
        level = donvi.Substring(0, 3);
        if (!IsPostBack)
        {
            if (level == "TDB")//tỉnh đoàn
            {
                lbltitlecsd.Visible = true;
                lbltitlecsd.Visible = true;
                drlchidoan.Visible = true;
                drlcosodoan.Visible = true;
                drlcosodoan.Items.Clear();
                ListItem item = new ListItem("---Chọn cơ sở đoàn---","-1");
                drlcosodoan.Items.Add(item);
                var list = _db.tblCoSoDoans.Where(r=>r.TrucThuoc==donvi);
                foreach (var items in list)
                {
                    item = new ListItem(items.TenDonVi.Trim(),items.MaDonVi.Trim());
                    drlcosodoan.Items.Add(item);
                }
                item = new ListItem("---Đoàn/chi đoàn cơ sở---", "-1");
                drldoancoso.Items.Add(item);
                item = new ListItem("---Chi đoàn---", "-1");
                drlchidoan.Items.Add(item);
            }
            else if (level == "CSD")//cơ sở đoàn
            {
                lbltitlecsd.Visible = false;
                drlcosodoan.Visible = false;
                lbltitledoancoso.Visible = true;
                drldoancoso.Visible = true;
                drlchidoan.Enabled = false;
                drldoancoso.Enabled = true;
                ListItem item = new ListItem("---Đoàn/chi đoàn cơ sở---", "-1");
                drldoancoso.Items.Add(item);
                var list = _db.tblCoSoDoans.Where(r => r.TrucThuoc == donvi);
                foreach (var items in list)
                {
                    item = new ListItem(items.TenDonVi.Trim(), items.MaDonVi.Trim());
                    drldoancoso.Items.Add(item);
                }
                item = new ListItem("---Chi đoàn---", "-1");
                drlchidoan.Items.Add(item);
            }
            else if (level == "DCS") //doan co so
            {
                lbltitlecsd.Visible = false;
                drlcosodoan.Visible = false;
                lbltitledoancoso.Visible = false;
                drldoancoso.Visible = false;
                lblchidoan.Visible = true;
                drlchidoan.Visible = true;
                drlchidoan.Enabled = true;
                drlchidoan.Items.Clear();
                ListItem item = new ListItem("---Chọn chi đoàn---", "-1");
                drlchidoan.Items.Add(item);
                var list = _db.tblCoSoDoans.Where(r => r.TrucThuoc == donvi);
                foreach (var items in list)
                {
                    item = new ListItem(items.TenDonVi.Trim(), items.MaDonVi.Trim());
                    drlchidoan.Items.Add(item);
                }
            }
            else //chi doan
            {
                lbltitlecsd.Visible = false;
                drlcosodoan.Visible = false;
                lbltitledoancoso.Visible = false;
                drldoancoso.Visible = false;
                lblchidoan.Visible = false;
                drlchidoan.Visible = false;
                btntruyvan.Visible = false;
            }
            this.BindGrid(donvi);
        }
    }
    //load du lieu vao gridview
    private void BindGrid(string madonvi)
    {
        _dbDataContext _db = new _dbDataContext();
        var val = from p in _db.tblDoanViens.Where(r => r.TrucThuoc.Trim() == madonvi)
                  select new MemberShip_ASP.QueryAll
                  {
                      MaDoanVien=p.MaDonVien.Trim(),
                      TenDoanVien = p.HoTen.Trim(),
                      TenDonVi=p.tblCoSoDoan.TenDonVi.Trim(),
                      Phai = (p.Phai == true ? "Nam" : "Nữ"),
                      NgaySinh = string.Format("{0:dd/MM/yyyy}", p.NgaySinh),
                      CMND = p.CMND.Trim(),
                      TrangThai=p.TrangThai 
                  };
        grid_truyvandoanvien.DataSource = val;
        grid_truyvandoanvien.DataBind();
    }
    //chọn all checkbox
    protected void chkInGridviewHeader_CheckedChanged(object sender, EventArgs e)//day la ham de goi trong header cua itemtemplate
    {
        CheckBox chk = (CheckBox)sender;
        CheckBox chkitem;
        foreach (GridViewRow dr in grid_truyvandoanvien.Rows)
        {
            chkitem = (CheckBox)dr.FindControl("chkchon");
            if (chk.Checked == true)
            {
                chkitem.Checked = true;
            }
            else
            {
                chkitem.Checked = false;
            }
        }
    }
    protected void btntruyvan_Click(object sender, EventArgs e)
    {
        if (level == "TDB") //new nguoi dang nhap la tinh doan
        {
            if (drlcosodoan.SelectedValue.ToString() == "-1")
            {
                BindGrid(donvi);
            }
            else
            {
                if (drldoancoso.SelectedValue.ToString() == "-1")
                {
                    BindGrid(drlcosodoan.SelectedValue.ToString());
                }
                else
                {
                    if (drlchidoan.SelectedValue.ToString() == "-1")
                    {
                        BindGrid(drldoancoso.SelectedValue.ToString());
                    }
                    else
                    {
                        BindGrid(drlchidoan.SelectedValue.ToString());
                    }
                }
            }
        }
        else if (level == "CSD")//neu nguoi dang nhap la co so doan
        {
            if (drldoancoso.SelectedValue.ToString() == "-1")
            {
                BindGrid(donvi);
            }
            else
            {
                if (drlchidoan.SelectedValue.ToString() == "-1")
                {
                    BindGrid(drldoancoso.SelectedValue.ToString());
                }
                else
                {
                    BindGrid(drlchidoan.SelectedValue.ToString());
                }
            }
        }
        else //neu nguoi dang nhap la doan co so, hoac chi doan co so
        {
            if (drlchidoan.SelectedValue.ToString() == "-1")
            {
                BindGrid(donvi);
            }
            else
            {
                BindGrid(drlchidoan.SelectedValue.ToString());
            }
        }
    }
    
    protected void grid_truyvandoanvien_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        for (int i = 0; i <= grid_truyvandoanvien.Rows.Count - 1; i++)
        {
            Label lblparent = (Label)grid_truyvandoanvien.Rows[i].FindControl("lbltrangthai");

            if (lblparent.Text == "1")
            {
                grid_truyvandoanvien.Rows[i].Cells[9].Text = "<button class='btn btn-circle btn-success show-tooltip' data-original-title='Đoàn viên'><i class='icon-user'></i></button>";
            }
            else if (lblparent.Text == "3")
            {
                grid_truyvandoanvien.Rows[i].Cells[9].Text = "<button class='btn btn-circle  btn-primary show-tooltip' data-original-title='Đoàn viên trưởng thành'><i class= 'icon-star '></i></button>";
            }
            else if (lblparent.Text == "2")
            {
                grid_truyvandoanvien.Rows[i].Cells[9].Text = "<button class='btn btn-circle btn-warning  show-tooltip' data-original-title='Đoàn viên dự bị'><i class='icon-bookmark-empty'></i></button>";
            }
            else if (lblparent.Text == "5")
            {
                grid_truyvandoanvien.Rows[i].Cells[9].Text = "<button class='btn btn-circle btn-danger  show-tooltip' data-original-title='Đoàn viên bị khai trừ'><i class='icon-cut'></i></button>";
            }
            else if (lblparent.Text == "8")
            {
                grid_truyvandoanvien.Rows[i].Cells[9].Text = "<button class='btn btn-circle btn-magenta  show-tooltip' data-original-title='Đoàn viên là cán bộ đoàn'><i class='icon-cog'></i></button>";
            }
        }
    }
    //chuc nang nay la xem chi tiet doan vien
    protected void grid_truyvandoanvien_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string id = grid_truyvandoanvien.DataKeys[e.RowIndex].Value.ToString();
        pncontent.Visible = false;
        pndetail.Visible = true;
        //xoa toan bo du lieu cu truoc khi dua du lieu moi vao
        var listOfUserControls = GetTextBoxControls(pndetail);
        foreach (TextBox ctrl in listOfUserControls)
        {
            ctrl.Text = "";
            ctrl.ReadOnly = true;
        }
        //dua du lieu vao
        var list = _db.tblDoanViens.Single(r => r.MaDonVien.Trim() == id.Trim());
        txtMaDoanVien1.Text = list.MaDonVien.Trim();
        txtHoten1.Text = list.HoTen.Trim();
        txtgioitinh.Text = (list.Phai == true ? "Nam" : "Nữ");
        txtngaysinh.Text = string.Format("{0:dd/MM/yyyy}", list.NgaySinh);
        txtCMND.Text = list.CMND;
        txtngaycap.Text = string.Format("{0:dd/MM/yyyy}", list.NgayCapCMND);
        txtNoiCap.Text = list.NoiCap;
        txtNguyenquan.Text = list.NguyenQuan;
        txtThuongTru.Text = list.HoKhauThuongTru;
        txtHienNay.Text = list.ChoOHienNay;
        txtDienthoai.Text = list.DienThoai;
        txtEmail.Text = list.Email;
        try { txtdantoc.Text = list.tblDanhMuc.TenDanhMuc; }
        catch { }
        try { txttongiao.Text = list.tblDanhMuc1.TenDanhMuc; }
        catch { }
        try { txthonnhan.Text = list.tblDanhMuc2.TenDanhMuc; }
        catch { }
        txtXuatthan.Text = list.XuatThan;
        txtNghenghiep.Text = list.NgheNghiep;
        txtMota.Text = list.MoTaCongViec;
        txtDiaBanDanCu.Text = (list.DiaBanDanCuID != null ? list.tblDanhMuc5.TenDanhMuc : "");
        txttrinhdo.Text = list.TrinhDoVanHoa;
        txttrinhdochuyenmon.Text = (list.TrinhDoChuyenMonID != null ? list.tblDanhMuc7.TenDanhMuc : "");
        txtdangvien.Text = ((bool)list.DangVien == true ? "Có" : "Không");
        txtChucVuDoan.Text = list.tblDanhMuc6.TenDanhMuc.Trim();
        txtbang1.Text = list.BangCap.Split('@')[0];
        txtbang2.Text = list.BangCap.Split('@')[1];
        try { txtngoaingu.Text = list.tblDanhMuc3.TenDanhMuc; }
        catch { }
        try { txttinhoc.Text = list.tblDanhMuc4.TenDanhMuc; }
        catch { }
        txtsuckhoe.Text = list.SucKhoe;
        txtngayvaodoan.Text = string.Format("{0:dd/MM/yyyy}", list.NgayVaoDoan);
        if (list.PhatTrienMoi == false)
        {
            txtphattrienmoi.Text = (list.PhatTrienMoi == true ? "Phát triển mới" : "Đơn vị khác chuyển đến");
            txtngaychuyenden.Text = string.Format("{0:dd/MM/yyyy}", list.NgayChuyenDen);
            txtDonViCu.Text = list.DonViCu;
            txtphattrienmoi.Visible = true;
            txtngaychuyenden.Visible = true;
            txtDonViCu.Visible = true;
        }
        else
        {
            txtphattrienmoi.Visible = false;
            txtngaychuyenden.Visible = false;
            txtDonViCu.Visible = false;
        }
    }
    protected void btnclose_Click(object sender, EventArgs e)
    {
        pncontent.Visible = true;
        pndetail.Visible = false;
    }
    public List<TextBox> GetTextBoxControls(Control ctrl)
    {
        var uCtrls = new List<TextBox>();
        foreach (Control child in ctrl.Controls)
        {
            if (child is TextBox) uCtrls.Add((TextBox)child);
            uCtrls.AddRange(GetTextBoxControls(child));
        }
        return uCtrls;
    }
    //visible row in grid
    protected void VisibleRowInGrid(string trangthai)
    {
        for (int i = 0; i <= grid_truyvandoanvien.Rows.Count - 1; i++)
        {
            Label lblparent = (Label)grid_truyvandoanvien.Rows[i].FindControl("lbltrangthai");
            if (lblparent.Text!=trangthai)
            {
                grid_truyvandoanvien.Rows[i].Visible = false;
            }
            else
            {
                grid_truyvandoanvien.Rows[i].Visible = true;
            }
        }
    }
    protected void linkDoanVienTruongThanh_Click(object sender, EventArgs e)
    {
        VisibleRowInGrid("3");
    }
    protected void linkDoanVienDuBi_Click(object sender, EventArgs e)
    {
        VisibleRowInGrid("2");
    }
    protected void linkDoanVienBiKhaiTru_Click(object sender, EventArgs e)
    {
        VisibleRowInGrid("5");
    }
    
    protected void btnexport_Click(object sender, EventArgs e)
    {
        Session.Remove("ListMaDoanVien_Print");
        Session.Remove("TenDonVi_Print");
        string listmadoanvien = "";
        CheckBox ckh;
        Label lbl;
        int count = 0;
        foreach (GridViewRow dr in grid_truyvandoanvien.Rows)
        {
            ckh = (CheckBox)dr.FindControl("chkchon");
            if (ckh.Checked == true)
            {
                lbl = (Label)dr.FindControl("lblMaDoanVien");
                listmadoanvien += lbl.Text.Trim() + "!";
                count++;
            }
        }
        if (count > 0)
        {
            Session["ListMaDoanVien_Print"] = listmadoanvien;
            Session["TenDonVi_Print"] = _ps.Decrypt(Request.QueryString["tid"].Replace(" ", "+"), "gold", 128);
            Response.Redirect("javascript:window.open('../Print/PrintPage.aspx','_blank');");
        }
        else
        {
            SecurityLib.ToolsMore.ShowPopUpMsg("Chưa chọn đoàn viên để in.", this.Page);
        }
    }

    protected void drlcosodoan_OnSelectedIndexChanged(object sender, EventArgs e)
    {
        drldoancoso.Items.Clear();
        var item = new ListItem("---Chọn đoàn/chi đoàn cơ sở---", "-1");
        drldoancoso.Items.Add(item);
        var list = _db.tblCoSoDoans.Where(r => r.TrucThuoc == drlcosodoan.SelectedValue.Trim());
        foreach (var items in list)
        {
            item = new ListItem(items.TenDonVi.Trim(), items.MaDonVi.Trim());
            drldoancoso.Items.Add(item);
        }
        drldoancoso.Enabled = drlcosodoan.SelectedValue.Trim() != "-1";
        item=new ListItem("---chọn chi đoàn---","-1");
        drlchidoan.Items.Add(item);
    }

    protected void drldoancoso_OnSelectedIndexChanged(object sender, EventArgs e)
    {
        drlchidoan.Items.Clear();
        var item = new ListItem("---Chọn chi đoàn---", "-1");
        drlchidoan.Items.Add(item);
        var list = _db.tblCoSoDoans.Where(r => r.TrucThuoc == drldoancoso.SelectedValue.Trim());
        foreach (var items in list)
        {
            item = new ListItem(items.TenDonVi.Trim(), items.MaDonVi.Trim());
            drlchidoan.Items.Add(item);
        }
        drlchidoan.Enabled = drldoancoso.SelectedValue.Trim() != "-1";
    }
    protected void linkDoanVien_Click(object sender, EventArgs e)
    {
        VisibleRowInGrid("1");
    }
    protected void linkcanbodoan_Click(object sender, EventArgs e)
    {
        VisibleRowInGrid("8");
    }
}